/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.discount.platform.task;


import cn.hutool.core.collection.CollUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yami.shop.discount.common.dao.DiscountMapper;
import com.yami.shop.discount.common.service.DiscountService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author yami
 */
@Slf4j
@Component
@AllArgsConstructor
public class DiscountTask {

    private final DiscountService discountService;
    private final DiscountMapper discountMapper;

    /**
     * 关闭已经结束的满减折活动
     */
    @XxlJob("closeDiscount")
    public void closeDiscount() {
        List<Long> shopIds = discountService.listShopIdsWhichWillClose();
        discountService.closeDiscountBySetStatus();
        if (CollUtil.isNotEmpty(shopIds)) {
            shopIds.forEach(discountService::removeDiscountsAndItemsCacheByShopId);
        }
    }

    /**
     * 满减活动开启，删除商家活动缓存
     */
    @XxlJob("startDiscount")
    public void startDiscount() {
        // 如果有活动在这1分钟内开始才去清除缓存
        List<Long> shopIds = discountMapper.listShopIdsWhichStart();
        if (CollUtil.isNotEmpty(shopIds)) {
            log.info("满减活动开始，清除缓存");
            // 清除缓存
            shopIds.forEach(discountService::removeDiscountsAndItemsCacheByShopId);
        }
    }
}
